<template>
  <div class="myContailer">
    <el-row class="myTitleBorderBottom">
      <el-col :span="12" class="myLineHeight_40px">{{this.title}}</el-col>
      <el-col :span="12" class="myTextAlignRight">
        <el-button type="primary" class="myBgGreen" @click="add" v-if="isShow(116)">新 增</el-button>
        <!-- <el-button type="primary" class="myBgGreen">导出Excel</el-button> -->
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px" :gutter="10">
      <el-col :span="6">
        <template>
          <el-cascader
            placeholder="全部项目/小区"
            :options="regionOptions"
            v-model="regionOptSelected"
            @change="changeRegionOpt"
          ></el-cascader>
        </template>
      </el-col>
      <el-col :span="7">
        <el-select v-model="buildingSelected"
          clearable
          @visible-change="clickBuildingSelect"
          placeholder="请选择楼栋">
          <el-option
            v-for="item in buildingList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" class="myBgGreen" @click="fuzzySearch">查 询</el-button>
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px">
      <el-col :span="24">
        <el-table
          ref="multipleTable"
          v-loading="loading"
          :data="list"
          :border = true
          :stripe = true
          style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            type="index"
            label="序号"
             :index="indexMethod"
            width="100"
            >
          </el-table-column>
          <el-table-column
            prop="district_name"
            label="所属片区"
            >
          </el-table-column>
          <el-table-column
            prop="area_name"
            label="所属小区"
            >
          </el-table-column>
          <el-table-column
            prop="cell_no"
            label="单元编号"
            >
          </el-table-column>
          <el-table-column
            prop="display_cell_name"
            label="单元名"
            >
          </el-table-column>
          <el-table-column
            prop="extra_desc"
            label="备注"
            >
          </el-table-column>
          <el-table-column
            label="操作"
            width="100">
            <template scope="scope">
              <el-button @click="getDetail(scope.row.cell_id)" class="look" type="text" size="small">查看</el-button> |
              <el-button type="text primary" @click="modify(scope.row.cell_id)" class="look" size="small" v-if="isShow(117)">编辑</el-button> 
              <!-- <el-button type="text"  data-toggle="modal" href="#delete" @click="deleteData(scope.row,scope.$index)" class="delete" size="small">删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>

        <div class="block">                    
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page_num"
            :page-sizes="[10, 15]"
            :page-size="page_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </el-col>
    </el-row>

    <!-- 确定删除弹框开始 -->
    <el-dialog title="确定删除?" class="myRoleDialog" :visible.sync="deleteDialog">
      <div>是否要删除单元名为{{lookObj.cell_name}}的信息！</div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="myBgGreen" @click="deleteDialog = false">取 消</el-button>
        <el-button type="primary" class="myBgGreen" @click="sureDelete">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 确定删除弹框结束 -->
  </div>
</template>

<style scoped>
  .myContailer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }
</style>

<script>
  import { getCellList } from '@/api/cell';
  import { getBuildingList } from '@/api/building';
  import { extractAreaIds } from '@/utils/misc';
  import { getRegion } from '@/utils/localstorage';

  export default {
    data(){
      return{  
        title:"单元管理",    
        list:[],  
        total:null, //总条数  
        page_num: null, //当前页码
        page_size: null, //每页显示多少条 

        selectedLd:"-1",
        ldList:[],
        building_id:-1,
        lookObj:{},
        pageNum: null, //页数
        page_size: null, //每页显示多少条
        page_num: null, //当前页码
        id:null,
        index:null,
        idArr:[],
        area_id: -1,
        pages:1000,
        loading:false,
        multipleSelection: [],

        buildingList: [],
        buildingLoaded: false,
        buildingSelected: null,
        buildingLoading: false,

        kw:"",
        regionOptions: [],
        regionOptSelected:[],
        deleteDialog:false
      }
    },
    created() {
      document.title=this.title;

      this.regionOptions = getRegion();
      this.regionOptSelected.push(this.regionOptions[0].value,this.regionOptions[0].children[0].value,this.regionOptions[0].children[0].children[0].value);
      
      this.fetchList();
    },

    computed: {
      areaIds() {
        return extractAreaIds(this.regionOptSelected, this.regionOptions);
      }
  },
    methods:{
       fetchList() {
        let vm = this;
        const params = {
          building_id: this.buildingSelected,
          page_size: this.page_size,
          page_num: this.page_num
        }

        vm.loading = true;
        getCellList(this.areaIds, params)
        .then(response => {
          let res = response.data;
          vm.list = res.rows;
          vm.total = res.total;
          vm.page_num = res.page_num;
          vm.page_size = res.page_size;

          vm.loading = false;
        })
        .catch(error => {
          vm.loading = false;
        });
      },
      fuzzySearch() {
         this.fetchList();
      },
      handleSizeChange(val) {
        this.page_size = val;
        this.fetchList();
      },
      handleCurrentChange(val) {
        let pageNum=window.localStorage.getItem("pageNum");
        if(pageNum){
          this.page_num = Number(pageNum);
          window.localStorage.removeItem("pageNum");
        }else{
          this.page_num = val;
        }
        this.fetchList();
      },
      changeRegionOpt() {
        this.buildingLoaded = false;
        this.buildingList = [];
        this.buildingSelected = null;
      },
      clickBuildingSelect() {
        let vm = this;
        if (this.regionOptSelected.length != 3) {
          return;
        }

        if (!this.buildingLoaded) {
          const params = {
            ril: 'S',
            page_num: 1,
            page_size: 999
          };

          vm.buildingLoading = true;
          getBuildingList(this.regionOptSelected[2], params)
          .then(response => {
            vm.buildingList = response.data.rows;
            vm.buildingLoaded = true;
          })
          .catch(error => {

          })
        }
      },

      add:function(){
        this.$router.push({
          path:'/dyglComonents',
          query: {type: 'add', pageNum: this.page_num}
        })
      },
      getDetail:function(cellId){
        this.$router.push({
          path:'/dyglComonents',
          query: {type: 'info', id: cellId, pageNum: this.page_num}
        })
      },

      modify:function(cellId){
        this.$router.push({
          path:'/dyglComonents',
          query: {type: 'update', id: cellId, pageNum: this.page_num}
        })
      },

      deleteData:function (datas,index) {
        this.idArr.push(datas.cell_id);
        this.index = index;
        this.lookObj = datas
      },
      changeSelect:function(select){
        this.building_id = select;
        if(this.building_id != -1){
          $(".errorDy").addClass("hide");
          $(".errorDy").closest(".col-md-6").find("input").removeClass("errorInput");
        }
      },
      sureDelete:function(){
        var pageArr = {
          page_size : this.page_size,
          page_num : this.currentPage,
        }
        if(this.list.length==1){
          var pageArr = {
            page_size : this.page_size,
            page_num : this.currentPage-1
          }
        }
        var arrId = {
          cell_ids : this.idArr.join(",")
        }
        optVue.deleteDyData(this,this.index,Tips,pageArr,arrId);
        $(".checkboxes").prop({checked:false})
      },
    
      allDelete:function(event){
     
        if(this.multipleSelection!=0){
          var pageArr = {
            page_num : this.currentPage-1
          }
          
          var arrId = {
            cell_ids : this.idArr.join(",")
          }

          optVue.deleteDyData(this,this.index,Tips,pageArr,arrId);
          
          this.idArr = [];
        }
        else{
          Tips.init({
            type:"fail",
            message:"请选择要删的项",
            class:"on"
          });
        }
      },
     
      handleSelectionChange(val) {
        this.multipleSelection = val;
        if(val.length!=0){
          for(var i = 0;i<val.length;i++){
            this.idArr.push(val[i].area_id)
          }
        }else{
          this.idArr = [];
        }
       
        //console.log(JSON.stringify(this.idArr))
      },
       indexMethod(index) {
          return (this.page_num-1)*this.page_size + index + 1
      }
    }

  }


</script>



